package JackSu.java01;

import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.file.Paths;
import java.text.NumberFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.*;

import static java.lang.Math.*;

public class JavaType {
    //类的初始化顺序：静态变量/静态代码块 -> main方法 -> 非静态变量/代码块 -> 构造方法

    //继承的初始化顺序：
    // 父类–静态变量/父类–静态初始化块
    //子类–静态变量/子类–静态初始化块
    //父类–变量/父类–初始化块
    //父类–构造器
    //子类–变量/子类–初始化块
    //子类–构造器

    enum Size {
        AAAA,
        BBBB,
        CCCC,
        DDDD
    }

    //strictfp标记采用严格的浮点计算。可能导致内存溢出
    @Test
    public strictfp void test01(){
        if(Double.isNaN(1.23/0)){
            System.out.println("true");
        }else{

            System.out.println("111\u00A0111");
            System.out.println("222\u0022+\u0022+222");
            System.out.println("false\u2122\u03C0");
        }
        System.out.println(Character.isJavaIdentifierStart('$'));//判断属于java的表示unicode的字母
        System.out.println(Character.isJavaIdentifierPart('~'));
        final double CM_PER_INCH=2.55;

        System.out.println(15.0/2);
    }

    /**
     * <strong>介绍Math类</strong>
     */
    @Test
    public void mathTest(){
        System.out.println(floorMod(-99,12));
        System.out.println(exp(2));
        System.out.println(log(Math.E));
        System.out.println(log10(100));
        System.out.println(PI);
        System.out.println(E);
        System.out.println(StrictMath.abs(-1));
        Size a = Size.AAAA;
        System.out.println(a);
        String join = String.join("/", "A", "B", "C", "D");
        System.out.println(join);
        int[] codeJoin = join.codePoints().toArray();
        System.out.println(Arrays.toString(codeJoin));
        String hello="Hello";
        int i = hello.lastIndexOf("l", 4);
        System.out.println(i);
        System.out.println(Math.round(2.55));
    }

    @Test
    public void formatTest(){
        String format01 = String.format("%+.2f", 33333.333);
        System.out.println(format01);
        String format02 = String.format("%,(.2f", -22222.222);
        System.out.println(format02);
        String format03 = String.format("%08d", 111);
        System.out.println(format03);
        String date04 = String.format("%tc", new Date());
        System.out.println(date04);
        String date05 = String.format("%1$s：%2$tF %2$tT","当前时间" ,new Date());//1$可以指出要被格式化的参数索引，紧跟%以$终止
        System.out.println(date05);
        String date06 = String.format("今天是一年中的第%tj天", new Date());
        System.out.println(date06);
        String date07 = String.format("%s：%tF %<tT %<tB %<tA","当前时间" ,new Date());//<标志指示前面的参数将被再次使用
        System.out.println(date07);
        NumberFormat numberFormat1=NumberFormat.getCurrencyInstance();//￥1.10，加了人民币符号。工厂方法
        NumberFormat numberFormat2=NumberFormat.getPercentInstance();//110%，加了百分号
        System.out.println(numberFormat1.format(1.1));
        System.out.println(numberFormat2.format(1.1));
    }

    @Test
    public void breakTest(){
        label:
        for(int j=1;j<4;j++) {
            System.out.println("打印1");
            for (int i = 0; i < 20; i++) {
                System.out.println("打印2");
                if (i == 10) {
                    break label;
                }
            }
        }
    }

    @Test
    public void BigDate(){
        BigInteger a = BigInteger.valueOf(10);//大数值
        BigInteger b = BigInteger.valueOf(5);
        BigInteger c = a.add(b);//+
        BigInteger d = c.multiply(b);//*
        BigInteger e = d.divide(c.add(a));// /
        BigInteger result = e.subtract(b);//-
        System.out.println(result);
        BigInteger mod = a.mod(b);//%
        System.out.println(mod);
        System.out.println(a.compareTo(d));//比较
        BigDecimal aa = BigDecimal.valueOf(9334534534541231113235.5);
        BigDecimal bb = BigDecimal.valueOf(348953853123131313452.3);
        BigDecimal cc = aa.add(bb);
        System.out.println(cc);
    }

    @Test
    public void arrTest(){
        int[] numbers={1,2,3,4,5,6,7,8,9,56,34,98,32,54};
        int[] copyNumbers=numbers;//直接将一个数组对象赋值给另一个，那么他们指向的对象是同一个
        System.out.println(Arrays.toString(copyNumbers));
        copyNumbers[0]=10;
        System.out.println(Arrays.toString(copyNumbers));
        System.out.println(Arrays.toString(numbers));
        numbers[1]=20;
        System.out.println(Arrays.toString(copyNumbers));
        System.out.println(Arrays.toString(numbers));
        Arrays.sort(numbers);//采用优化的快排
        System.out.println(Arrays.toString(numbers));
        int number = Arrays.binarySearch(numbers, 0,numbers.length,34);//采用二分查找
        System.out.println(number);
        Arrays.fill(numbers,9);
        System.out.println(Arrays.toString(numbers));
    }

    @Test
    public void arrTwoTest(){
        int[][] balances={{1,1,1,1,1},{2,2,2,2,2,2},{3,3,3,3}};
        System.out.println(Arrays.deepToString(balances));//打印二维数组
    }

    @Test
    public void localDateTest(){
        LocalDate now = LocalDate.now();
        System.out.println(now);
        LocalDate date = LocalDate.of(1998, 12, 31);
        System.out.println(date);
        LocalDate date1 = date.plusDays(10);//加
        int year = date1.getYear();
        int monthValue = date1.getMonthValue();
        int dayOfMonth = date1.getDayOfMonth();
        System.out.println(year+"/"+monthValue+"/"+dayOfMonth);
        LocalDate date2 = date.minusDays(10);//减
        System.out.println(date2);
        LocalDate date3 = date.withMonth(2);
        System.out.println(date3);
        Date date4 = new Date();
        Object clone = date4.clone();
        System.out.println(clone);
    }

    @Test
    public void getReportDate(){
        String format = String.format("%tY%<tm%<td%<tH%<tM%<tS", new Date());//YYYYMMDDHHmmss。String的格式化方法<代表引用前面的，还可以用%1$tY、%2$tm代表引用后面第一位参数和第二位参数
        System.out.println(format);
        ZonedDateTime now = LocalDateTime.of(9999, 12, 31, 0, 0, 1).atZone(ZoneOffset.UTC);
        String s = now.toString().replaceAll("-", "").replaceAll(" ", "").replaceAll(":", "").replaceAll("T","").replaceAll("Z","");
        System.out.println(s);

        Integer a=3333333;
        int b=3333333;
        Integer c=3333333;
        HashSet<Integer> sets = new HashSet<Integer>(){{add(a);add(b);add(c);}};
        ArrayList<Integer> lists = new ArrayList<>(sets);
        System.out.println(Arrays.toString(lists.toArray()));
    }

    @Test
    public void finalTest() throws FileNotFoundException {
        final StringBuffer buffer=new StringBuffer();//final关键字只是表示拥有该修饰词的对象引用不能够再指向其他对象。
        buffer.append("1111");
//        buffer=new StringBuffer();
        String str=buffer.toString();
        System.out.println(str);
        System.setOut(new PrintStream("d:/java"));//System.out是一个静态常量，setOut可以将其引用指向其他对象是因为该方法是一个本地方法而并非java语言实现。
    }

    public static void main(String[] args) throws IOException {
        {
            Scanner scanner1 = new Scanner(Paths.get("d:\\java\\1.txt"), "GBK");
            System.out.println(scanner1.toString());
            System.out.println(System.getProperty("user.dir"));//Java虚拟机启动路径
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("name:");
        String s1 = scanner.nextLine();
//        Console console = System.console();
//        String s2 = console.readLine("username:");
//        char[] chars = console.readPassword("password:");
//        System.out.println(s1+s2+Arrays.toString(chars));
    }
}
